#include "PathBuilder.h"
#include <sys/stat.h>
#include <dirent.h>

PathBuilder::PathBuilder(std::string path)
{
    this->fileFullPath_ = appendSlash(path.c_str());
}

void PathBuilder::buildPaths(std::string filePath, std::vector<FilePathData>& pathVector)
{
    this->fileRelativePath_ += getSimpleName(filePath.c_str());

    if (isDirectory(filePath.c_str()))
    {
        DIR* directory = opendir(filePath.c_str());
        struct dirent* ent;
        if (directory)
        {

            FilePathData pathDataDir;
            pathDataDir.fullPath_ += this->fileFullPath_;
            pathDataDir.fullPath_ += this->fileRelativePath_;
            pathDataDir.relativePath_ = this->fileRelativePath_;
            pathVector.push_back(pathDataDir);

            while ((ent = readdir(directory)) != NULL)
            {
                std::string fileName = ent->d_name;

                if (fileName.compare(".") != 0 && fileName.compare("..") != 0)
                {
                    std::string currentFilePath = this->fileFullPath_;
                    this->fileRelativePath_ = appendSlash(this->fileRelativePath_.c_str());
                    currentFilePath += this->fileRelativePath_;
                    currentFilePath += fileName;

                    if (isDirectory(currentFilePath.c_str()))
                    {
                        currentFilePath += "/";
                    }
                    buildPaths(currentFilePath, pathVector);
                }
            }
            this->fileRelativePath_ = backToRelativePath(this->fileRelativePath_);
        }
    }
    else
    {
        FilePathData filePathData;
        filePathData.relativePath_ = this->fileRelativePath_;
        filePathData.fullPath_ = this->fileFullPath_;
        filePathData.fullPath_ += this->fileRelativePath_;
        pathVector.push_back(filePathData);
        this->fileRelativePath_ = backToRelativePath(this->fileRelativePath_);
    }
}
